package com.gt.xmall.web;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gt.xmall.log.model.SysDataLog;
import com.gt.xmall.model.SysUser;
import com.gt.xmall.service.AccountService;
import com.gt.xmall.util.StringUtil;


@Controller
@RequestMapping(value = "/user/")
public class UserController extends BaseController{

	
	@Autowired
	private AccountService accountService;
	
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list( Model model){
		log.info("userList>>>"+accountService.getAllUser());
		model.addAttribute("userList", accountService.getAllUser());
		
		return "userlist";
		
	}
	
	@RequestMapping(value = "show/{id}",method = RequestMethod.GET)
	public String show(@PathVariable("id") String id,Model model){
		log.info("sysUser>>>"+accountService.getUser(new Long(id)));
		model.addAttribute("sysUser", accountService.getUser(new Long(id)));
		
		return "userForm";
		
	}
	
	@RequestMapping(value="update",method = RequestMethod.POST)
	public String update(@Valid SysUser user, RedirectAttributes redirectAttributes) {
		
		accountService.updateUser(user);
		redirectAttributes.addFlashAttribute("name", user.getName());
		return "redirect:/login";
	}
}
